package com.coin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coin.domain.Config;
import com.coin.mapper.ConfigMapper;
import com.coin.service.ConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Auther: 李 力
 * @Date: 2025/6/6
 * @Description: com.coin.service.impl
 * @version: 1.0
 */
@Service
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements ConfigService {

    /**
     * @Description: 参数配置查询
     * @Author: 李 力
     * @Date: 2025/6/6
     * @Param:
     * @return: R
     */
    @Override
    public Page<Config> findByPage(Page<Config> page, String type, String name, String code) {
        LambdaQueryWrapper<Config> wrapper = new LambdaQueryWrapper<>();
        wrapper = wrapper.like(StrUtil.isNotEmpty(type), Config::getType, type)
                .like(StrUtil.isNotEmpty(name), Config::getName, name)
                .like(StrUtil.isNotEmpty(code), Config::getCode, code);
        return page(page, wrapper);
    }

    /**
     * @Description: 新增一个参数
     * @Author: 李 力
     * @Date: 2025/6/6
     * @Param: config
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveConfig(Config config) {
        if (config == null) {
            throw new RuntimeException("参数为null");
        }
        return save(config);
    }

    /**
     * @Description: 修改一个参数
     * @Author: 李 力
     * @Date: 2025/6/6
     * @Param: config
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateConfig(Config config) {
        if (config == null) {
            throw new RuntimeException("参数为空");
        }
        return updateById(config);
    }
}
